package com.ruoyi.system.interAttr.attr;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class YUNCANGAttr implements AdapterAttr {

    /**
     * 计算Sign
     *
     * @param postList postList
     * @return sign
     */
    private static String getSign(Map<String, Object> postList, String key) {
        Map<String, Object> treeMap = MapUtil.sort(postList);
        StringBuilder stringBuilder = new StringBuilder();
        // stringBuilder.append(key);
        for (Map.Entry<String, Object> entry : treeMap.entrySet()) {
            if (entry.getValue() == null) {
                continue;
            }
            if (StrUtil.isEmpty(entry.getValue().toString())) {
                continue;
            }
            stringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
        }
        stringBuilder.append(key);
        return SecureUtil.md5(stringBuilder.toString()).toUpperCase();
    }

    public static void main(String[] args) {
        Map<String, Object> params = new HashMap<>();
        params.put("merchantId", "41");
        params.put("gameId", 315);
        params.put("goodsTypeId", "1");

        String sign = getSign(params, "key=@IfqADw65wdXV9VSGJt6yNuU^Kf8l&No");
        String url = "https://www.cyjh.co/api/v1/yc-share-common/upper-conf-list?"+ "merchantId=" + 41
                + "&gameId=" + 315
                + "&goodsTypeId=" + 1
                + "&sign=" + sign;
        String response = HttpRequest.get(url)
                .execute().body();
        System.out.println(response);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        params.put("merchantId", "41");
        String sign = getSign(params, "key=@IfqADw65wdXV9VSGJt6yNuU^Kf8l&No");
        String url = "https://www.cyjh.co/api/v1/yc-share-common/game-list?"+ "merchantId=" + 41 + "&sign=" + sign;
        String response = HttpRequest.get(url)
                .execute().body();
        String data = JSONObject.parseObject(response).getString("data");
        String list = JSONObject.parseObject(data).getString("list");
        List<JSONObject> jsonObjects = JSONObject.parseArray(list, JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
//        Map<String, Object> params = new HashMap<>();
//        params.put("merchantId", "41");
//        params.put("gameId", gameId);
//        params.put("goodsTypeId", "1");
//
//        String sign = getSign(params, "key=@IfqADw65wdXV9VSGJt6yNuU^Kf8l&No");
//        String url = "https://www.cyjh.co/api/v1/yc-share-common/upper-conf-list?"+ "merchantId=" + 41
//                + "&gameId=" + gameId
//                + "&goodsTypeId=" + 1
//                + "&sign=" + sign;
//        String response = HttpRequest.get(url).execute().body();
//
//        String data2 = JSONObject.parseObject(response).getString("data");
//        String list = JSONObject.parseObject(data2).getString("list");
//        List<JSONObject> data = JSONObject.parseArray(list, JSONObject.class);
//        data = data.stream().filter(j -> Objects.equals(j.getString("name"), "游戏区服")).collect(Collectors.toList());
//        String options = data.get(0).getString("options");
//        List<JSONObject> data3 = JSONObject.parseArray(options, JSONObject.class);
        List<InterServerRes> interServerRes = new ArrayList<>();
//        for (JSONObject datum : data3) {
            InterServerRes interServer = new InterServerRes();
            interServer.setUuid(UUIDGenerator.generateUuid6());
            interServer.setId("不匹配区服!");
            interServer.setName("不匹配区服!");
//            interServer.setId(datum.getString("id"));
//            interServer.setName(datum.getString("name"));
//            String children = datum.getString("children");
//            if(StringUtils.isNotEmpty(children)) {
//                List<InterServerRes> interServerRes1 = new ArrayList<>();
//                List<JSONObject> jsonObjects = JSONObject.parseArray(children, JSONObject.class);
//                for (JSONObject object : jsonObjects) {
//                    InterServerRes interServer1 = new InterServerRes();
//                    String id1 = object.getString("id");
//                    String title1 = object.getString("name");
//                    interServer1.setUuid(UUIDGenerator.generateUuid6());
//                    interServer1.setId(id1);
//                    interServer1.setName(title1);
//                    interServerRes1.add(interServer1);
//                }
//                interServer.setInterServerRes(interServerRes1);
//            }
            interServerRes.add(interServer);
//        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String, Object> params = new HashMap<>();
        params.put("merchantId", "41");
        params.put("gameId", gameId);
        params.put("goodsTypeId", "1");

        String sign = getSign(params, "key=@IfqADw65wdXV9VSGJt6yNuU^Kf8l&No");
        String url = "https://www.cyjh.co/api/v1/yc-share-common/upper-conf-list?"+ "merchantId=" + 41
                + "&gameId=" + gameId
                + "&goodsTypeId=" + 1
                + "&sign=" + sign;
        String response = HttpRequest.get(url).execute().body();

        String data2 = JSONObject.parseObject(response).getString("data");
        String list = JSONObject.parseObject(data2).getString("list");
        List<JSONObject> data = JSONObject.parseArray(list, JSONObject.class);
        data = data.stream().filter(j -> !Objects.equals(j.getString("name"), "游戏区服")).collect(Collectors.toList());

        List<InterAttrRes> interAttrResList = new ArrayList<>();
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("htmlType").equals("select")&&d.getString("isMultipleChoice").equals("0")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("htmlType").equals("select")&&d.getString("isMultipleChoice").equals("1")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> d.getString("htmlType").equals("inputNumber")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {

            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setUuid(UUIDGenerator.generateUuid6());
            interAttrRes.setId(object.getString("pingyin"));
            interAttrRes.setName(object.getString("name"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            String string = object.getString("options");
            JSONArray jsonArray = JSONArray.parseArray(string);
            if(StringUtils.isNotEmpty(string)) {
                for (Object o : jsonArray) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setUuid(UUIDGenerator.generateUuid6());
                    interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("id"));
                    interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("name"));
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}